// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameClient;
import zombie.network.PacketTypes;

public final class IsoObjectSyncRequests {
    public final ArrayList<IsoObjectSyncRequests.SyncData> requests = new ArrayList();
    public long timeout = 1000L;

    public void putRequest(IsoGridSquare square, IsoObject obj) {
        if (GameClient.bClient) {
            this.putRequest(square.x, square.y, square.z, (byte)square.getObjects().indexOf(obj));
        }
    }

    public void putRequestLoad(IsoGridSquare square) {
        if (GameClient.bClient) {
            this.putRequest(square.x, square.y, square.z, (byte)square.getObjects().size());
        }
    }

    public void putRequest(int x, int y, int z, byte objIndex) {
        if (zombie.SystemDisabler.doObjectStateSyncEnable) {
            IsoObjectSyncRequests.SyncData syncData = new IsoObjectSyncRequests.SyncData();
            syncData.x = x;
            syncData.y = y;
            syncData.z = z;
            syncData.objIndex = objIndex;
            syncData.reqTime = 0L;
            syncData.reqCount = 0;
            synchronized (this.requests) {
                this.requests.add(syncData);
            }
        }
    }

    public void sendRequests(UdpConnection connection) {
        if (zombie.SystemDisabler.doObjectStateSyncEnable) {
            if (this.requests.size() != 0) {
                ByteBufferWriter byteBufferWriter = connection.startPacket();
                PacketTypes.PacketType.SyncIsoObjectReq.doPacket(byteBufferWriter);
                ByteBuffer byteBuffer = byteBufferWriter.bb;
                int int0 = byteBuffer.position();
                byteBufferWriter.putShort((short)0);
                int int1 = 0;
                synchronized (this.requests) {
                    for (int int2 = 0; int2 < this.requests.size(); int2++) {
                        IsoObjectSyncRequests.SyncData syncData = (IsoObjectSyncRequests.SyncData)this.requests.get(int2);
                        if (syncData.reqCount > 4) {
                            this.requests.remove(int2);
                            int2--;
                        } else {
                            if (syncData.reqTime == 0L) {
                                syncData.reqTime = System.currentTimeMillis();
                                int1++;
                                byteBuffer.putInt(syncData.x);
                                byteBuffer.putInt(syncData.y);
                                byteBuffer.putInt(syncData.z);
                                byteBuffer.put(syncData.objIndex);
                                syncData.reqCount++;
                            }

                            if (System.currentTimeMillis() - syncData.reqTime >= this.timeout) {
                                syncData.reqTime = System.currentTimeMillis();
                                int1++;
                                byteBuffer.putInt(syncData.x);
                                byteBuffer.putInt(syncData.y);
                                byteBuffer.putInt(syncData.z);
                                byteBuffer.put(syncData.objIndex);
                                syncData.reqCount++;
                            }

                            if (int1 >= 50) {
                                break;
                            }
                        }
                    }
                }

                if (int1 == 0) {
                    GameClient.connection.cancelPacket();
                } else {
                    int int3 = byteBuffer.position();
                    byteBuffer.position(int0);
                    byteBuffer.putShort((short)int1);
                    byteBuffer.position(int3);
                    PacketTypes.PacketType.SyncIsoObjectReq.send(GameClient.connection);
                }
            }
        }
    }

    public void receiveIsoSync(int x, int y, int z, byte objIndex) {
        synchronized (this.requests) {
            for (int _int = 0; _int < this.requests.size(); _int++) {
                IsoObjectSyncRequests.SyncData syncData = (IsoObjectSyncRequests.SyncData)this.requests.get(_int);
                if (syncData.x == x && syncData.y == y && syncData.z == z && syncData.objIndex == objIndex) {
                    this.requests.remove(_int);
                }
            }
        }
    }

    private class SyncData {
        int x;
        int y;
        int z;
        byte objIndex;
        long reqTime;
        int reqCount;
    }
}
